Lists¶

🎨 Arithmetic Operators¶

In [1]:
7 + 2
Out[1]:
9
In [2]:
7 - 2
Out[2]:
5
In [3]:
7 * 2
Out[3]:
14
In [4]:
7 ** 2
Out[4]:
49

7 ** 2 is $ 7 ^ 2 $

In [5]:
7 / 2
Out[5]:
3.5
In [6]:
7 // 2
Out[6]:
3
In [7]:
7 % 2
Out[7]:
1

$\frac{7}{2} = 3r1$

7 // 2 gives us the $3$

7 % 2 gives us the $1$ (the remainder)

🎨 Comparisons¶

In [8]:
2 > 1
Out[8]:
True
In [9]:
2 > 2
Out[9]:
False
In [10]:
2 <= 2
Out[10]:
True
In [11]:
2 >= 2
Out[11]:
True
In [14]:
4 == 4
Out[14]:
False
In [13]:
4 != 6 
Out[13]:
True

🎨 None¶

What is the value of a variable that doesn't have a value?

What does a function return when it doesn't return anything?

In [15]:
def no_return():
    print('This function doesn\'t return anything.')
    

value = no_return()

print('the value is:', value)
This function doesn't return anything.
the value is: None
In [16]:
type(value)
Out[16]:
NoneType

None is what Python uses to communicate nothing.

It's what you use to indicate that you don't have any information.

In [17]:
thing = 8
thing is None
Out[17]:
False
In [18]:
thing = None
thing is None
Out[18]:
True

To determine whether a variable is None, use the is None expression.

In [19]:
thing = 9
thing is not None
Out[19]:
True

To determine whether a variable is not None, use the is not None expression.

🎨 []¶

🖌 .append(...)¶

append: to put after, to place at the end

In [20]:
names = []
while True:
    name = input("Give me a name: ")
    if name == '':
        break
    names.append(name)
    print(names)

print(names)
Give me a name: Charles
['Charles']
Give me a name: George
['Charles', 'George']
Give me a name: Sandy
['Charles', 'George', 'Sandy']
Give me a name: Walter
['Charles', 'George', 'Sandy', 'Walter']
Give me a name: Ezekiel
['Charles', 'George', 'Sandy', 'Walter', 'Ezekiel']
Give me a name: 
['Charles', 'George', 'Sandy', 'Walter', 'Ezekiel']
  • [] creates an empty list
  • .append(...) adds the item to the list
  • you can see the contents by printing the list
    • Note the commas
  • What happens when you enter no names (enter '' the first time)?
In [21]:
names = ['Julia', 'Juan', 'George', 'Gina']
print(names)
['Julia', 'Juan', 'George', 'Gina']

You can define a list with initial contents.

In [22]:
names = ['Julia', 'Juan', 'George', 'Gina']
while True:
    name = input("Give me a name: ")
    if name == '':
        break
    names.append(name)
    
print(names)
Give me a name: Fred
Give me a name: Phred
Give me a name: Gordon
Give me a name: Ivan
Give me a name: 
['Julia', 'Juan', 'George', 'Gina', 'Fred', 'Phred', 'Gordon', 'Ivan']

You can add to a list that started with contents already.

🎨 len¶

In [23]:
fruit = ['apple', 'peach', 'pear', 'açaí']
how_many = len(fruit)
print(how_many)
print(len(fruit))
4
4

You can find the length, or size, of a list using the len function.

🖌 for¶

In [26]:
students = ['Julia', 'Juan', 'George', 'Gina', 'Gina']

for student in students:
    print(f"Hello {student}. Welcome to CS 110.")
    print(f"I hope you ({student}) have a great day.")
Hello Julia. Welcome to CS 110.
I hope you (Julia) have a great day.
Hello Juan. Welcome to CS 110.
I hope you (Juan) have a great day.
Hello George. Welcome to CS 110.
I hope you (George) have a great day.
Hello Gina. Welcome to CS 110.
I hope you (Gina) have a great day.
Hello Gina. Welcome to CS 110.
I hope you (Gina) have a great day.

👩🏻‍🎨 Fruit Salad¶

Write a program that:

  1. Asks the user for a series of fruit
  • The user inputs '' (empty string) when they are done
  1. Print the list of fruits with a "~" bullet in front of each fruit.

fruit_salad.py¶

In [ ]:
%%file for_class/fruit_salad.py
# 🍓 🍎 🥭

👨🏻‍🎨 Bullet styles¶

Write a program that:

  1. Asks the user for a series of items
  • The user inputs '' (empty string) when they are done
  1. Print the list of items using the following bullets:
  • *
  • -
  • >

bullet_styles.py¶

👩🏽‍🎨 Big and Small¶

Write a program that queries the user for a list of numbers (one number at a time).

Then ask the user what number to use as the boundary between "big" and "small" numbers.

Print "You have {how_many} numbers:"

Then print out all the numbers with one of the following comments:

  • for "big" numbers say "(big)"
  • for "small" numbers say "(small)"
Number: 8
Number: 10
Number: 30
Number: 2
Number: 65
Number: 42
Number: 13
Number: 77
Number: 
Boundary: 20
You have 8 numbers:
- 8 (small)
- 10 (small)
- 30 (big)
- 2 (small)
- 65 (big)
- 42 (big)
- 13 (small)
- 77 (big)

Key Ideas¶

  • Arithmetic operators
  • Lists!
  • []
  • .append(...)
  • len
  • for